import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = np.array([])
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey =np.append(sKey, temp)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def cal_region_attri(region_id):
    dis_reg = np.unique(region_id)
    iLen = dis_reg.shape[0]
    temp_reg_attri = np.zeros((iLen, 3)) #[caner, pop, rate]

    # unit_attri = [cancer, pop, area, rate]

    i = 0
    for item in unit_attri:
        temp_reg_attri[int(region_id[i]),0] += item[0]
        temp_reg_attri[int(region_id[i]),1] += item[1]
        #temp_reg_attri[int(region_id[i]),2] + = item[2]
        i = i + 1

    for item in temp_reg_attri:
        item[2] = item[0]/item[1]

    return temp_reg_attri[:,3]


def cal_riskare_attri():
    temp_popcancer = np.zeros((len(riskID),3))
    for item in unit_attri:
        temp_popcancer[int(item[0]),0] += item[1]
        temp_popcancer[int(item[0]),1] += item[2]
    temp_popcancer[:,2] = temp_popcancer[:,0]/temp_popcancer[:,1]
    temp_popcancer.shape = (-1, 3)
    #print temp_popcancer
    return temp_popcancer

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    
    unitCSV = "C:/_DATA/CancerData/test/Mar21/pvalue/16_4polygon/TP1000_16_4polygon.csv"
    dataMatrix = build_data_list(unitCSV)  # [ID, pop, riskID, cancer1, cancer2, cancer3]
    iLen = dataMatrix.shape
    unit_attri = np.zeros((iLen[0],3))
    unit_attri[:,0] = dataMatrix[:,2]
    unit_attri[:,2] = dataMatrix[:,1]
    riskID = np.unique(unit_attri[:,0])
    #print unit_attri
    repeat = 1
    riskarea_attri = np.array([])

    while repeat < 31:
        unit_attri[:,1] = dataMatrix[:,repeat+2] # [riskID, cancer, pop]
        temp_riskarea_attri = cal_riskare_attri()  # [total_cancer, total_pop, total_rate]
        repeat = repeat + 1
        riskarea_attri = np.append(riskarea_attri, temp_riskarea_attri)
    riskarea_attri.shape = (-1,len(riskID)*3)
    print riskarea_attri
    riskarea_attri = np.transpose(riskarea_attri)
    #print riskarea_attri
    
    filePath = unitCSV[:-4] + "_riskarea_attri.csv"
    np.savetxt(filePath, riskarea_attri, delimiter=',')

    print "end at " + getCurTime()
    print "========================================================================"  

           
